package de.amaly.wosdemdes.activity;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import de.amaly.wosdemdes.R;
import de.amaly.wosdemdes.data.ShowProgressEntry;
import de.amaly.wosdemdes.persistence.ShowProgressEntryDAO;

/**
 * An activity allowing to create a new {@link ShowProgressEntry} or edit an existing {@link ShowProgressEntry} and
 * persist the result.
 * 
 * @author Andreas Maly
 * 
 */
public class CreateEditShowProgress extends Activity {

    /** DAO reference for accessing the database */
    private ShowProgressEntryDAO dao;

    /** The ShowProgressEntry this Activity is currently editing */
    private ShowProgressEntry currentShowProgressEntry = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_edit_show_progress);
        // Initialize the database DAO
        dao = new ShowProgressEntryDAO(this);

        // Initialize the Spinner values
        Spinner seasonSpinner = (Spinner) findViewById(R.id.spinnerSelectSeasonNumber);
        Integer[] seasonChoices = ArrayUtils.toObject(getResources().getIntArray(R.array.choices_season_spinner));
        ArrayAdapter<Integer> seasonSpinnerAdapter = new ArrayAdapter<Integer>(this,
                android.R.layout.simple_spinner_item, seasonChoices);
        seasonSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        seasonSpinner.setAdapter(seasonSpinnerAdapter);

        Spinner episodeSpinner = (Spinner) findViewById(R.id.spinnerSelectEpisodeNumber);
        Integer[] episodeChoices = ArrayUtils.toObject(getResources().getIntArray(R.array.choices_episode_spinner));
        ArrayAdapter<Integer> episodeSpinnerAdapter = new ArrayAdapter<Integer>(this,
                android.R.layout.simple_spinner_item, episodeChoices);
        episodeSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        episodeSpinner.setAdapter(episodeSpinnerAdapter);

        Spinner discSpinner = (Spinner) findViewById(R.id.spinnerSelectDiscNumber);
        Integer[] discChoices = ArrayUtils.toObject(getResources().getIntArray(R.array.choices_disc_spinner));
        ArrayAdapter<Integer> discSpinnerAdapter = new ArrayAdapter<Integer>(this,
                android.R.layout.simple_spinner_item, discChoices);
        discSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        discSpinner.setAdapter(discSpinnerAdapter);

        // Has a ShowProgressEntry been passed for editing, or do we have to create a new one?
        Intent intent = getIntent();
        currentShowProgressEntry = (ShowProgressEntry) intent
                .getSerializableExtra(ShowsListActivity.EXTRA_SHOW_PROGRESS_ENTRY);

        // If no ShowProgressEntry has been passed, create a new one
        if (currentShowProgressEntry == null) {
            currentShowProgressEntry = new ShowProgressEntry();
            setTitle(R.string.create_watch_progress);

            // If a ShowProgressEntry has been passed, transfer its values to the input views
        } else {
            setTitle(R.string.edit_watch_progress);

            EditText editShowName = (EditText) findViewById(R.id.editShowName);
            editShowName.setText(currentShowProgressEntry.getShowName());

            Spinner spinnerSelectSeasonNumber = (Spinner) findViewById(R.id.spinnerSelectSeasonNumber);
            ArrayAdapter<Integer> adapter = (ArrayAdapter) spinnerSelectSeasonNumber.getAdapter();
            int spinnerPosition = adapter.getPosition(currentShowProgressEntry.getSeasonNumber());
            spinnerSelectSeasonNumber.setSelection(spinnerPosition);

            Spinner spinnerSelectEpisodeNumber = (Spinner) findViewById(R.id.spinnerSelectEpisodeNumber);
            adapter = (ArrayAdapter) spinnerSelectEpisodeNumber.getAdapter();
            spinnerPosition = adapter.getPosition(currentShowProgressEntry.getEpisodeNumber());
            spinnerSelectEpisodeNumber.setSelection(spinnerPosition);

            Spinner spinnerSelectDiscNumber = (Spinner) findViewById(R.id.spinnerSelectDiscNumber);
            adapter = (ArrayAdapter) spinnerSelectDiscNumber.getAdapter();
            spinnerPosition = adapter.getPosition(currentShowProgressEntry.getDiscNumber());
            spinnerSelectDiscNumber.setSelection(spinnerPosition);

            EditText editProgressComment = (EditText) findViewById(R.id.editProgressComment);
            editProgressComment.setText(currentShowProgressEntry.getProgressComment());
        }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.create_edit_show_progress, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_saveEntry) {

            // Set all edited values in the currentShowProgressEntry
            EditText editShowName = (EditText) findViewById(R.id.editShowName);
            if (!StringUtils.isBlank(editShowName.getText())) {
                currentShowProgressEntry.setShowName(editShowName.getText().toString());
            } else {
                // The show's name is mandatory; if it is missing, don't save the entry
                Toast errorMsg = Toast.makeText(this, R.string.error_show_name_is_mandatory, Toast.LENGTH_LONG);
                errorMsg.show();
                return true;
            }

            Spinner spinnerSelectSeasonNumber = (Spinner) findViewById(R.id.spinnerSelectSeasonNumber);
            if (spinnerSelectSeasonNumber.getSelectedItem() != null) {
                currentShowProgressEntry.setSeasonNumber(Integer.valueOf(spinnerSelectSeasonNumber.getSelectedItem()
                        .toString()));
            }

            Spinner spinnerSelectEpisodeNumber = (Spinner) findViewById(R.id.spinnerSelectEpisodeNumber);
            if (spinnerSelectEpisodeNumber.getSelectedItem() != null) {
                currentShowProgressEntry.setEpisodeNumber(Integer.valueOf(spinnerSelectEpisodeNumber.getSelectedItem()
                        .toString()));
            }

            Spinner spinnerSelectDiscNumber = (Spinner) findViewById(R.id.spinnerSelectDiscNumber);
            if (spinnerSelectDiscNumber.getSelectedItem() != null) {
                currentShowProgressEntry.setDiscNumber(Integer.valueOf(spinnerSelectDiscNumber.getSelectedItem()
                        .toString()));
            }

            EditText editProgressComment = (EditText) findViewById(R.id.editProgressComment);
            if (!StringUtils.isBlank(editProgressComment.getText())) {
                currentShowProgressEntry.setProgressComment(editProgressComment.getText().toString());
            }

            dao.open();
            dao.saveShowProgressEntry(currentShowProgressEntry);
            dao.close();

            // Finish this activity and return to the previous one
            this.setResult(RESULT_OK);
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
